;;; TwoBears response.lisp
;; Copyright (c) 2014 Kalman Kiss, Zalaegerszeg Hungary
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;;
;; Author and contact:
;; Kalman Kiss <kiskami@freemail.hu>
;; 8900 Zalaegerszeg, Hungary
;; Kakukkfu u. 4.

(in-package :twobears)

;;; Grizzly (Http)Response functions

(defparameter *grizzly-response-class* (java:jclass "org.glassfish.grizzly.http.server.Response"))
(defparameter *java-string-class* (java:jclass "java.lang.String"))
(defparameter *java-int-class* (java:jclass "int"))
(defparameter *java-ioWriter-class* (java:jclass "java.io.Writer"))

(defparameter *grizzly-response-setconttype-method* (java:jmethod *grizzly-response-class* "setContentType" *java-string-class*))
(defparameter *grizzly-response-addheader-method* (java:jmethod *grizzly-response-class* "addHeader" *java-string-class* *java-string-class*))
(defparameter *grizzly-response-setcontlen-method* (java:jmethod *grizzly-response-class* "setContentLength" *java-int-class*))
(defparameter *grizzly-response-getwriter-method* (java:jmethod *grizzly-response-class* "getWriter"))

(defparameter *java-ioWriter-write-method* (java:jmethod *java-ioWriter-class* "write" *java-string-class*))

(defun set-resp-content-type_ (resp type)
  (#"setContentType" resp type))

(defun add-resp-header_ (resp header value)
  (#"addHeader" resp header value))

(defun resp-write_ (resp str)
  (#"setContentLength" resp (length str))
  (#"write" (#"getWriter" resp) str))

(defun set-resp-content-type (resp type)
  (java:jcall *grizzly-response-setconttype-method* resp type))

(defun add-resp-header (resp header value)
  (java:jcall *grizzly-response-addheader-method* resp header value))

(defun resp-write (resp str)
  (java:jcall *grizzly-response-setcontlen-method* resp (length str))
  (java:jcall *java-ioWriter-write-method* (java:jcall *grizzly-response-getwriter-method* resp) str))
